package de.gora.prog2swing.adventskalender;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

/**
 * Different revisions available on http://code.google.com/p/prog2swing/source/list
 * 
 * @author Marco Gora
 * @email marco.gora@student.htw-berlin.de
 * @version Java 7u5, Eclipse Juno (4.x)
 */
public class AdventCalendar extends JFrame {
	
	/**
	 * eclipse specific serialVersionId
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Attributes
	 */
	private JPanel[] doors;
	private JLabel 
		infoLabel,
		imageLabel;
	private Container cPane;
	private Date date;
	private JMenuBar menu;
	
	public AdventCalendar(){
		this("Kalender");
	}
	
	public AdventCalendar(String title){
		super(title);
		this.init();
	}
	
	public void init(){
		//start title thread
		Time time = new Time(this);
		Thread timeThread = new Thread(time);
		
		timeThread.start();
		
		//initialize locals
		ImageIcon image = PicTool.loadImage(new File("./img/image2.gif"));
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.YYYY");
		
		GridBagLayout 
			calendarLayout = new GridBagLayout(),
			infoLayout = new GridBagLayout(),
			imageLayout = new GridBagLayout();
		
		JPanel 
			pSouth = new JPanel(),
		    pEast = new JPanel(),
		    pCenter = new JPanel(),
		    infoPanel = new JPanel(),
		    imagePanel = new JPanel();
		
		JLabel
			labelAdv = new JLabel("Adventskalender");
		
		GridBagConstraints 
			doorConstraint[] = new GridBagConstraints[24],
			labelAdvConstraint = new GridBagConstraints(),
			infoConstraint = new GridBagConstraints(),
			imageConstraint = new GridBagConstraints();
		

		//initialize attributes
		this.doors = new JPanel[24];
		this.date = new Date();
		this.infoLabel = new JLabel("Heute ist der " + dateFormat.format(this.date));
		this.imageLabel = new JLabel(image);
		this.menu = new AdventMenu();
		
		//Display
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.cPane = this.getContentPane();
		this.cPane.setLayout(new BorderLayout());
		this.cPane.add(pSouth, BorderLayout.SOUTH);
		this.cPane.add(pEast, BorderLayout.EAST);
		this.cPane.add(pCenter, BorderLayout.CENTER);
		
		labelAdvConstraint.gridwidth = GridBagConstraints.REMAINDER;
		infoConstraint.fill = GridBagConstraints.BOTH;
		imageConstraint.fill = GridBagConstraints.NONE;
	
		infoPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		infoPanel.add(this.infoLabel);
		imagePanel.add(this.imageLabel);
		
		pCenter.setLayout(imageLayout);
		pEast.setLayout(calendarLayout);
		pSouth.setLayout(infoLayout);
		pSouth.add(infoPanel, infoConstraint);
		pEast.add(labelAdv, labelAdvConstraint);
		
		pCenter.add(imagePanel, imageConstraint);
		
		for(int i = 0; i < 24; i++){
			doorConstraint[i] = new GridBagConstraints();
			doorConstraint[i].fill = GridBagConstraints.BOTH;
			doorConstraint[i].gridy = i / 4 * 10 + 10;
			
			this.doors[i] = new JPanel();
			this.doors[i].add(new JLabel(String.valueOf(i + 1)));
			this.doors[i].setBorder(BorderFactory.createLineBorder(Color.black));
			this.doors[i].addMouseListener(new DoorAdapter(this, i));
			pEast.add(this.doors[i], doorConstraint[i]);
		}
		
		this.setJMenuBar(this.menu);
		this.setVisible(true);
		this.pack();
	}

	public JPanel[] getDoors() {
		return doors;
	}

	public Container getcPane() {
		return cPane;
	}

	public Date getDate() {
		return date;
	}

	public JLabel getInfoLabel() {
		return infoLabel;
	}
}